
<%@ page language="java" contentType="text/html" 
    pageEncoding="UTF-8" %>
    

<%@ page import="javax.xml.transform.*" %>
<%@ page import="javax.xml.transform.stream.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.MalformedURLException" %>

<%@ page import="java.net.HttpURLConnection" %>

<% 

  String subject = request.getParameter("subject").replace(" ","%20");
  URL url_subject = new URL("http://api.powerhousemuseum.com/api/v1/subject/xml/?api_key=90b0723f9926ce3&name_exact="+subject);
  HttpURLConnection conn = (HttpURLConnection) url_subject.openConnection();

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  
  Document doc_subject = db.parse(conn.getInputStream());
  
  //Get itemsUri
  String itemsUri = doc_subject.getElementsByTagName("items_uri").item(0).getFirstChild().getNodeValue();
  String itemsId = doc_subject.getElementsByTagName("id").item(0).getFirstChild().getNodeValue();
  
  URL url_items= new URL("http://api.powerhousemuseum.com"+itemsUri + "&order_by=title");
  out.println(url_items);
  out.println(itemsId);
  
  //Using xsl 
  Transformer transformer = TransformerFactory .newInstance().newTransformer(new StreamSource(application.getResourceAsStream("items.xsl")));	
  transformer.setParameter("pageUrl","subject/"+itemsId+"/items");
  transformer.transform(new StreamSource(url_items.openStream()), new StreamResult(out));
  
%>

